package top.hkyzf.study.leetcode.math;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-3-2 8:36
 */
public class 寻找最近的回文数_564 {
    public String nearestPalindromic(String n) {
        long longN = Long.parseLong(n);
        // 特殊情况小于10
        if (longN <= 10) {
            return String.valueOf(longN-1);
        }
        // 假设原数据为 abcde ，需要对比的值有  abcbc ab(c+1)bc ab(c-1)bc abcde+1 abcde-1 abcde+2 abcde-2，求最小值
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        StringBuilder sb3 = new StringBuilder();
        // 偶数
        int half = n.length() / 2;
        if (n.length() % 2 == 0) {
            for (int i = 0; i < n.length(); i++) {
                if (i < half-1) {
                    sb1.append(n.charAt(i));
                    sb2.append(n.charAt(i));
                    sb3.append(n.charAt(i));
                } else if (i == half-1) {
                    sb1.append((char) (n.charAt(i)-1));
                    sb2.append(n.charAt(i));
                    sb3.append((char) (n.charAt(i)+1));
                } else if (i == half) {
                    char ch = n.charAt(n.length() - 1 - i);
                    sb1.append((char) (ch-1));
                    sb2.append(ch);
                    sb3.append((char) (ch+1));
                } else {
                    char ch = n.charAt(n.length() - 1 - i);
                    sb1.append(ch);
                    sb2.append(ch);
                    sb3.append(ch);
                }
            }
        } else {
            for (int i = 0; i < n.length(); i++) {
                if (i < half) {
                    sb1.append(n.charAt(i));
                    sb2.append(n.charAt(i));
                    sb3.append(n.charAt(i));
                } else if (i == half) {
                    sb1.append((char) (n.charAt(i)-1));
                    sb2.append(n.charAt(i));
                    sb3.append((char) (n.charAt(i)+1));
                } else {
                    char ch = n.charAt(n.length() - 1 - i);
                    sb1.append(ch);
                    sb2.append(ch);
                    sb3.append(ch);
                }
            }
        }
        long l1 = longN-1;
        long l2 = longN+1;
        long l3 = longN-2;
        long l4 = longN+2;
        if (check(l1)) {
            return String.valueOf(l1);
        }
        if (check(l2)) {
            return String.valueOf(l2);
        }
        if (check(l3)) {
            return String.valueOf(l3);
        }
        if (check(l4)) {
            return String.valueOf(l4);
        }
        return check(sb1, sb2, sb3, n);
    }

    private boolean check(long l) {
        String string = String.valueOf(l);
        StringBuilder sb = new StringBuilder(string);
        String reverse = sb.reverse().toString();
        return string.equals(reverse);
    }

    private String check(StringBuilder sb1, StringBuilder sb2, StringBuilder sb3, String n) {
        long l1 = 0;
        long l3 = 0;
        boolean mark1 = true;
        boolean mark3 = true;
        try {
            l1 = Long.parseLong(sb1.toString());
        } catch (NumberFormatException ignored) {
            mark1 = false;
        }
        try {
            l3 = Long.parseLong(sb3.toString());
        } catch (NumberFormatException ignored) {
            mark3 = false;
        }
        long l2 = Long.parseLong(sb2.toString());
        long ln = Long.parseLong(n);
        long abs1 = Long.MAX_VALUE;
        long abs2 =  Long.MAX_VALUE;
        long abs3 = Long.MAX_VALUE;
        if (mark1) {
            abs1 = Math.abs(l1 - ln);
        }
        if (mark3) {
            abs3 = Math.abs(l3 - ln);
        }
        if (Math.abs(l2 - ln) != 0) {
            abs2 = Math.abs(l2 - ln);
        }
        long min = Math.min(abs1, abs2);
        min = Math.min(min, abs3);
        if (min == abs1) {
            return String.valueOf(l1);
        }
        if (min == abs2) {
            return String.valueOf(l2);
        }
        return String.valueOf(l3);
    }

    @Test
    public void testNearestPalindromic () {
        String n = "807045053224792883";
        System.out.println(nearestPalindromic(n));
    }
}
